import java.io.*;
import java.net.*;
import java.util.LinkedList;

import utils.Compteur;

import evaluation.Evaluateur;

import modele.AbsPiece;
import modele.Pousser;
import modele.Pousseur;
import mouvement.GenerateurMouvement;


class Client {
    public static void main(String[] args) {
         
    Socket MyClient;
    BufferedInputStream input;
    BufferedOutputStream output;
    int[][] board = new int[8][8];
    char[] colonne = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
    GenerateurMouvement gM;
    
    try {
        MyClient = new Socket("localhost", 8888);
        input    = new BufferedInputStream(MyClient.getInputStream());
        output   = new BufferedOutputStream(MyClient.getOutputStream());
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); 
        gM = new GenerateurMouvement();
        
        while(1 == 1){
            char cmd = 0;
            
            cmd = (char)input.read();
                    
            // Début de la partie en joueur blanc
            if(cmd == '1'){
                Compteur.start();
                
                byte[] aBuffer = new byte[1024];
                Evaluateur.setWhitePlayer(true);
                gM = new GenerateurMouvement();
                int size = input.available();
                //System.out.println("size " + size);
                input.read(aBuffer,0,size);
                String s = new String(aBuffer).trim();
                String[] boardValues;
                boardValues = s.split(" ");
                int x=0,y=0;
                for(int i=0; i<boardValues.length;i++){
                    board[x][y] = Integer.parseInt(boardValues[i]);
                    
                    if(board[x][y] == 4) {
                        AbsPiece piece = new Pousseur(String.valueOf(colonne[x]) + String.valueOf(8 - y), board[x][y]);
                        gM.addPiece(String.valueOf(colonne[x]) + String.valueOf(8 - y), piece);
                    } else if(board[x][y] == 3){
                        AbsPiece piece = new Pousser(String.valueOf(colonne[x]) + String.valueOf(8 - y) , board[x][y]);
                        gM.addPiece(String.valueOf(colonne[x]) + String.valueOf(8 - y), piece);
                    }
                    
                    x++;
                    if(x == 8){
                        x = 0;
                        y++;

                    }
                }
                
                System.out.print(gM.toString());

                System.out.println("Nouvelle partie! Vous jouer blanc, entrez votre premier coup : ");
                String move = null;
                LinkedList<String> mouvements = gM.getPossibilites(board);
                System.out.println("Movements possible : " + mouvements.size());
                //move = console.readLine();
                move = Evaluateur.evaluateBestMove(mouvements,board);
                output.write(move.getBytes(),0,move.length());
                gM.bouge(move);
                
                //updateBoard(s);
                String mcase1 = move.substring(0,2);
                String mcase2 = move.substring(2,4);
                
                int mcases1Col = mcase1.charAt(0) - 'A';
                int mcases1Lig = 8 - Integer.parseInt(String.valueOf(mcase1.charAt(1)));
                int mcases2Col = mcase2.charAt(0) - 'A';
                int mcases2Lig = 8 - Integer.parseInt(String.valueOf(mcase2.charAt(1)));

                board[mcases2Col][mcases2Lig] = board[mcases1Col][mcases1Lig];
                board[mcases1Col][mcases1Lig] = 0;
                
                output.flush();
            }
            // Début de la partie en joueur Noir
            if(cmd == '2'){

                Evaluateur.setWhitePlayer(false);
                System.out.println("Nouvelle partie! Vous jouer noir, attendez le coup des blancs");
                byte[] aBuffer = new byte[1024];
                gM = new GenerateurMouvement();
                
                int size = input.available();
                //System.out.println("size " + size);
                input.read(aBuffer,0,size);
                String s = new String(aBuffer).trim();
                String[] boardValues;
                boardValues = s.split(" ");
                int x=0,y=0;
                for(int i=0; i<boardValues.length;i++){
                    board[x][y] = Integer.parseInt(boardValues[i]);
                    System.out.print( board[x][y]);
                    
                    if(board[x][y] == 2) {
                        AbsPiece piece = new Pousseur(String.valueOf(colonne[x]) + String.valueOf(8 - y), board[x][y]);
                        gM.addPiece(String.valueOf(colonne[x]) + String.valueOf(8 - y), piece);
                    } else if(board[x][y] == 1){
                        AbsPiece piece = new Pousser(String.valueOf(colonne[x]) + String.valueOf(8 - y) , board[x][y]);
                        gM.addPiece(String.valueOf(colonne[x]) + String.valueOf(8 - y), piece);
                    }
                    
                    x++;
                    
                    if(x == 8){
                        x = 0;
                        y++;
                        System.out.println();
                    }
                }
                String move = null;
                
                LinkedList<String> mouvements = gM.getPossibilites(board);
                System.out.println("Movements possible : " + mouvements.size());
                //move = console.readLine();
                //move = Evaluateur.evaluateBestMove(mouvements,board);
                
            }


            // Le serveur demande le prochain coup
            // Le message contient aussi le dernier coup joué.
            if(cmd == '3'){
                Compteur.start();
                
                byte[] aBuffer = new byte[16];
                System.out.print(gM.toString());
                int size = input.available();
                //System.out.println("size " + size);
                input.read(aBuffer,0,size);
                
                String s = new String(aBuffer);
                System.out.println("Dernier coup : "+ s);
                System.out.println("Entrez votre coup : ");
                String move = null;

                //updateBoard(s);
                String case1 = s.substring(1,3);
                String case2 = s.substring(6,8);
                
                int cases1Col = case1.charAt(0) - 'A';
                int cases1Lig = 8 - Integer.parseInt(String.valueOf(case1.charAt(1)));
                int cases2Col = case2.charAt(0) - 'A';
                int cases2Lig = 8 - Integer.parseInt(String.valueOf(case2.charAt(1)));

                board[cases2Col][cases2Lig] = board[cases1Col][cases1Lig];
                board[cases1Col][cases1Lig] = 0;
                
                gM.removePiece(case2);
                
                LinkedList<String> mouvements = gM.getPossibilites(board);
                System.out.println("Movements possible : " + mouvements.size());
                //move = console.readLine();

                move = Evaluateur.evaluateBestMove(mouvements, board);
                System.out.println("Mouv :" + move);
                
                output.write(move.getBytes(),0,move.length());
                gM.bouge(move);
                
                //updateBoard(s);
                String mcase1 = move.substring(0,2);
                String mcase2 = move.substring(2,4);
                
                int mcases1Col = mcase1.charAt(0) - 'A';
                int mcases1Lig = 8 - Integer.parseInt(String.valueOf(mcase1.charAt(1)));
                int mcases2Col = mcase2.charAt(0) - 'A';
                int mcases2Lig = 8 - Integer.parseInt(String.valueOf(mcase2.charAt(1)));

                board[mcases2Col][mcases2Lig] = board[mcases1Col][mcases1Lig];
                board[mcases1Col][mcases1Lig] = 0;
                
                output.flush();
                
            }
            // Le dernier coup est invalide
            if(cmd == '4'){
                System.out.println("Coup invalide, entrez un nouveau coup : ");
                System.out.println("Board : ");
                for(int i = 0; i < board.length; i ++) {
                    
                    for(int j = 0; j < board[i].length; j ++) {
                        System.out.print(" " + board[i][j]);
                    }
                    System.out.println();
                }
                
                String move = null;
                move = console.readLine();
                output.write(move.getBytes(),0,move.length());
                output.flush();
                
            }
        }
    }
    catch (IOException e) {
        System.out.println(e);
    }
    
    }
}

